package testing.com;

/**
 * Author: $Author$
 * File: $HeadURL$
 * Date Created: 17 June 2010
 * Date Modified: $Date$ 
 * Version: $Rev$
 * 
 * Description:
 * Create a basic calculator application for input/output
 */

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Calc2 extends Activity {
    LinearLayout linearHandle;
    TableLayout tableLayout;
    TableRow tableRow;
    
    TextView display;
    Button button;
    
    LinearLayout.LayoutParams linearParams;
    TableLayout.LayoutParams tableParams;
    TableRow.LayoutParams rowParams;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //create a handle to xml layout by id in R.java
        //layoutHandle = (LinearLayout)findViewById(R.id.layout);
        linearHandle = (LinearLayout)findViewById(R.id.linear);
        //tableHandle = (TableLayout)findViewById(R.id.table);
        //rowHandle = (TableRow)findViewById(R.id.row);
        
        //create content
        buildInterface();
    }
    
    public void buildInterface() {
        //define parameters
        linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
        rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        
        //create layout views
        tableLayout = new TableLayout(this);
        tableLayout.setStretchAllColumns(true);
        
        //display view
        display = new TextView(this);
        display.setLines(1);
        display.setTextSize(50);
        display.setGravity(Gravity.RIGHT);
        
        linearHandle.addView(display, linearParams);
        linearHandle.addView(tableLayout, linearParams);
        
        createButtonArray();
    }

    public void createButtonArray() {
        int btnCounter = 0;
        String buttons[] = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
        //rows
        for(int i=0; i<4; i++) {
            tableRow = new TableRow(this);
            tableRow.setGravity(Gravity.CENTER);
            //columns
            for(int j=0; j<4; j++) {
                //button = (Button) findViewById(R.id.gridview);
                button = new Button(this);                    
                //button.setId(counter);
                button.setContentDescription(buttons[btnCounter]);
                button.setText(buttons[btnCounter++]); 
                button.setTextSize(30);
                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // Perform action on click
                        //String id = Integer.toString(v.getId());
                        display.append(v.getContentDescription());
                    }
                });
                tableRow.addView(button, rowParams);
            }
            tableLayout.addView(tableRow, tableParams);
        }
        tableRow = new TableRow(this);
        tableRow.setGravity(Gravity.CENTER);
        button = new Button(this);
        button.setContentDescription("Clear");
        button.setText("Clear");
        button.setTextSize(30);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                display.setText("");
            }
        });
        linearHandle.addView(button, linearParams);       
    }
}